/**
 * tzdesk系统平台
 * moonvip_admin
 * com.tz.service.goodcatogory
 * IGoodcatogoryService.java
 * 创建人:小胡 
 * 时间：2016年12月06日 22:54:38
 *
 */
package com.tz.service.goodcatogory.impl;

import com.tz.bean.Goodcatogory;
import com.tz.bean.TzParams;
import com.tz.dao.goodcatogory.IGoodcatogoryMapper;
import com.tz.service.goodcatogory.IGoodcatogoryService;
import com.tz.util.TmStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

/**
 * 平台商品分类管理serviceImpl
 * IGoodcatogoryService
 * 创建人:小胡 
 * 时间：2016年12月06日 22:54:38
 * @version 1.0.0
 * 
 */
@Service
public class GoodcatogoryServiceImpl implements IGoodcatogoryService{

	@Autowired
	private IGoodcatogoryMapper goodcatogoryMapper;


	//子类
	TreeMap<String,List<HashMap<String, Object>>> childrenMap = null;

	//map<root , List<Map>>
	/*根权限目录的遍历*/


	@Override
	public List<HashMap<String , Object>>findGoodcatogorys(TzParams params){
		if(TmStringUtils.isEmpty(params.getOrder()))params.setOrder("a.create_time desc");
		return goodcatogoryMapper.findGoodcatogorys(params);
	}
	
	@Override
	public int count(TzParams params) {
		return goodcatogoryMapper.count(params);
	}

	@Override
	public HashMap<String,Object> getGoodcatogory(Integer id) {
		return goodcatogoryMapper.getGoodcatogory(id);
	}

	@Override
	public int save(Goodcatogory goodcatogory) {
		return goodcatogoryMapper.save(goodcatogory);
	}

	@Override
	public int update(Goodcatogory goodcatogory) {
		return goodcatogoryMapper.update(goodcatogory);
	}

	@Override
	public int delete(TzParams params) {
		return goodcatogoryMapper.delete(params);
	}

	//查询一级分类
	public List<HashMap<String ,Object>> getFirstCatogary(){
		return goodcatogoryMapper.getFirstCatogary();
	}

	@Override
	public List<HashMap<String, Object>> getSecondCatogary(TzParams params) {
		return goodcatogoryMapper.getSecondCatogary(params);
	}

	//查询是否有该二级分类了
	public int getCountBysecondName(TzParams params){
		return goodcatogoryMapper.getCountBysecondName(params);
	}

	@Override
	public int getCountByfirstName(TzParams params) {
		return goodcatogoryMapper.getCountByfirstName(params);
	}

	@Override
	public HashMap<String, Object> findCatogories(TzParams params) {
		//返回类型
		HashMap<String, Object> datas = new HashMap<>();
		//装载所有的子元素
		childrenMap = new TreeMap<>();
		//所有的根目标
		List<Goodcatogory> catogories = goodcatogoryMapper.findCatogoryRoot(params);
		if(catogories!=null && catogories.size()>0){
			List<HashMap<String, Object>> maps = new ArrayList<>();//泛型的菱形写法 jdk1.7
			for (Goodcatogory catogory : catogories) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("name", catogory.getName());
				map.put("url", "javascript:void(0);");
				map.put("opid", catogory.getId());
				map.put("pid", catogory.getId());
				maps.add(map);
				getChildrenData(catogory.getId());
			}
			datas.put("root", maps);
			datas.put("children", childrenMap);
			return datas;
		}else{
			return null;
		}
	}

	private void getChildrenData(Integer pid){
		//查询所有的子类
		List<Goodcatogory> childrenCatogories = goodcatogoryMapper.findCatogoryChildren(pid);
		//判断是否还有子类对象
		if(childrenCatogories!=null && childrenCatogories.size()>0){
			//数据类型接受
			List<HashMap<String, Object>> maps = new ArrayList<>();//泛型的菱形写法 jdk1.7
			//开始循环所有子类
			for (Goodcatogory catogory : childrenCatogories) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("name", catogory.getName());
				map.put("url", "javascript:void(0);");
				map.put("opid", catogory.getId());
				map.put("pid", catogory.getId());
				//添加到返回值中
				maps.add(map);
				getChildrenData(catogory.getId());
			}
			childrenMap.put(pid+"", maps);
		}
	}
}
